package controllers

import (
	"book-management-system/services"
	"github.com/gin-gonic/gin"
	"net/http"
)

func BorrowBook(c *gin.Context) {
	userID := c.Param("user_id")
	bookID := c.Param("book_id")

	if err := services.BorrowBook(userID, bookID); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Book borrowed"})
}

func ReturnBook(c *gin.Context) {
	userID := c.Param("user_id")
	bookID := c.Param("book_id")

	if err := services.ReturnBook(userID, bookID); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Book returned"})
}

func GetUserBorrowedBooks(c *gin.Context) {
	userID := c.Param("user_id")

	books, err := services.GetUserBorrowedBooks(userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, books)
}
